package com.hhx.dao.impl;
import com.hhx.dao.UserDao;
import com.hhx.entity.User;
import com.hhx.utils.BaseDao;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.ArrayList;
import java.util.List;
public class UserDaoImpl implements UserDao {
    private final JdbcTemplate template = new JdbcTemplate( BaseDao.getDataSource());
    @Override
    public User findByUsername(String username) {
        try {
            String sql = "select * from user where username = ?";
            List<User> users = template.query(sql, new Object[]{username}, new BeanPropertyRowMapper<>(User.class));
            return users.isEmpty()? null : users.get(0);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    @Override
    public boolean authenticate(String username, String password) {
        try {
            String sql = "select * from user where username = ? and password = ?";
            List<User> users = template.query(sql, new Object[]{username, password}, new BeanPropertyRowMapper<>(User.class));
            return !users.isEmpty();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


}
